package cassandrawindow;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HandleLogBannerDomainV2 {

    public void handleData()
    {
        MysqlDataManagerMain mysqlMain = new MysqlDataManagerMain();
        MysqlDataManager mysql = new MysqlDataManager();
        //String currentDate = TimeUtil.getCurrentTimeInString();//2012-07-24
        String currentDate = HandleBalanceAndPromotionCPC.handleDate();

        //String currentDate = CONSTANT.Date;//2012-07-24
        //match channelId and domain
        Map<String,String> matchChannelAndDomain    =  new HashMap<>();
        matchChannelAndDomain = mysqlMain.getMatchChannelAndDomain();
        //get click corresponding with banner && domain
        Map<String,String> banDomain    =   new HashMap<>();
        banDomain = mysql.getBannerDomain(currentDate);//banner_domain_dt:totalclick_totalview
        //get all banner running
        Map<String,String> allBannerRunning =   new HashMap<>();

        allBannerRunning = mysqlMain.getAllBannerRunning();//banner_listchannelid
        //get all bannner trong log
        Map<String,String> allBannerLog =   new HashMap<>();
        allBannerLog = mysql.getAllBannerRunning(currentDate);//banner:domain_totalclick_totalview
        //tinh click view trong log        
        Iterator iterator = allBannerRunning.entrySet().iterator();
        
        StringBuilder sbstr  = new StringBuilder();
	while (iterator.hasNext()) {
            Map.Entry mapEntry  = (Map.Entry) iterator.next();
            String bannerid     = (String) mapEntry.getKey();
            String listChannel    = (String) mapEntry.getValue();

            if(allBannerLog.containsKey(bannerid))
            {               
                try {
                    String valueHang = getValueHang(bannerid, currentDate, listChannel, matchChannelAndDomain, banDomain);
                    String clickHang = valueHang.split("_")[0];
                    String viewHang = valueHang.split("_")[1];
                    String listSite = valueHang.split("_")[2];

                    String value = allBannerLog.get(bannerid);
                    String domain = value.split("_")[0];
                    String clickLog = value.split("_")[1];
                    String viewLog = value.split("_")[2];

                    String strResult = "(" + bannerid + ",'" + listSite + "','" + domain + "'," +clickHang +"," + clickLog + "," + viewHang +"," + viewLog +",'" + currentDate +"')";
                    sbstr.append(strResult).append(",");

                    if(sbstr.toString().length() > 50000)
                    {
                            //xu li cho du lieu cuoi
                            String relStr = sbstr.toString();
                            String valInsert = relStr.substring(0, (relStr.length()-1));
                            mysql.insertDataDomainDataBannerLog(valInsert);
                            sbstr.setLength(0);
                    }
                    
                } catch (Exception ex){
                    System.out.println("error :"+ex.getMessage());
                }
            }
                
	}

        if(sbstr.toString().length() > 0)
        {
            // MysqlDataManager mysql = new MysqlDataManager();
            //xu li cho du lieu cuoi
            String relStr = sbstr.toString();
            String valInsert = relStr.substring(0, (relStr.length()-1));
            mysql.insertDataDomainDataBannerLog(valInsert);
            sbstr.setLength(0);
        }        
        //tinh click view voi nhung domain ma khach hang treo
    }

    public String getValueHang(String bannerid ,String currentDate, String str, Map matchChannelAndDomain,Map banDomain){
        String valRet = "";
        int click =0;
        int view =0;
        String[] arrValue = new String[200];
        arrValue = str.split(",");
        StringBuilder list_site = new StringBuilder();
        
        for (int i = 0 ;i < arrValue.length;i++)
        {                        
            if(matchChannelAndDomain.containsKey(arrValue[i])){
                String domain = (String) matchChannelAndDomain.get(arrValue[i]);
                if(!list_site.toString().contains(domain))
                {
                    list_site.append(domain).append(",");
                    String key = bannerid +"_"+domain+"_"+currentDate;
                    
                    if(banDomain.containsKey(key)){
                        String value = (String) banDomain.get(key);
                        String totalclick = value.split("_")[0];
                        String totalview = value.split("_")[1];
                        click   = click + Integer.valueOf(totalclick);
                        view    = view + Integer.valueOf(totalview);
                    }
                }
                               
            }
        }
        valRet = click+"_"+view+"_"+list_site.toString();
        return valRet;
    }
}
